# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@bazel_gazelle//:def.bzl", "gazelle")
load("@com_github_bazelbuild_buildtools//buildifier:def.bzl", "buildifier")
load("@io_bazel_rules_docker//container:container.bzl", "container_image")
load("//bazel:repositories.bzl", "list_pl_deps")
load("//bazel/cc_toolchains:llvm_libs.bzl", "llvm_variant_repo_name", "llvm_variant_setting_label", "llvm_variants")
load("//bazel/cc_toolchains/sysroots:images.bzl", "sysroot_runtime_image", "sysroot_test_image")
load("//bazel/external/ubuntu_packages:packages.bzl", "packages")

licenses(["restricted"])

exports_files(["LICENSE"])

gazelle(
    name = "gazelle",
    prefix = "px.dev/pixie",
)

# This line is necessary to keep Gazelle from using the vendored gRPC for Go library:
# gazelle:exclude vendor
# gazelle:exclude third_party
# gazelle:exclude src/ui/node_modules
# gazelle:go_naming_convention import
# gazelle:exclude **/*.pb.go
# gazelle:exclude **/mock.go
# gazelle:exclude external

# Make gazelle not generate proto files. We need to use gogo proto and this does
# not seem to work automatically right now. Keep an eye out on issue:
# https://github.com/bazelbuild/bazel-gazelle/issues/181 for updates.
# gazelle:proto disable

# TODO(zasgar): We should keep an eye on fixes to gazelle that make this automatic
# Make gazelle resolve the proto paths correctly.
# gazelle:resolve go px.dev/pixie/src/api/proto/cloudpb //src/api/proto/cloudpb:cloudapi_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/api/proto/uuidpb //src/api/proto/uuidpb:uuid_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/api/proto/vispb //src/api/proto/vispb:vis_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/api/proto/vizierpb //src/api/proto/vizierpb:vizier_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/api/proto/vizierconfigpb //src/api/proto/vizierconfigpb:vizier_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/carnotpb //src/carnot/carnotpb:carnot_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/docspb //src/carnot/docspb:docs_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/planner/compilerpb //src/carnot/planner/compilerpb:compiler_status_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/planner/distributedpb //src/carnot/planner/distributedpb:distributed_plan_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/planner/dynamic_tracing/ir/logicalpb //src/carnot/planner/dynamic_tracing/ir/logicalpb:logical_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/planner/plannerpb //src/carnot/planner/plannerpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/planpb //src/carnot/planpb:plan_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/queryresultspb //src/carnot/queryresultspb:query_results_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/carnot/udfspb //src/carnot/udfspb:udfs_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/artifact_tracker/artifacttrackerpb //src/cloud/artifact_tracker/artifacttrackerpb:artifact_tracker_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/auth/authpb //src/cloud/auth/authpb:auth_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/auth/authpb //src/cloud/auth/authpb:auth_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/config_manager/configmanagerpb //src/cloud/config_manager/configmanagerpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/cron_script/cronscriptpb //src/cloud/cron_script/cronscriptpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/plugin/pluginpb //src/cloud/plugin/pluginpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/profile/profilepb //src/cloud/profile/profilepb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/project_manager/projectmanagerpb //src/cloud/project_manager/projectmanagerpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/scriptmgr/scriptmgrpb //src/cloud/scriptmgr/scriptmgrpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/shared/messagespb //src/cloud/shared/messagespb:messages_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/vzconn/vzconnpb //src/cloud/vzconn/vzconnpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/cloud/vzmgr/vzmgrpb //src/cloud/vzmgr/vzmgrpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/common/base/statuspb //src/common/base/statuspb:status_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/protocol_loadtest/grpc/loadtestpb //src/e2e_test/protocol_loadtest/grpc/loadtestpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/artifacts/versionspb //src/shared/artifacts/versionspb:versions_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/bloomfilterpb //src/shared/bloomfilterpb:bloomfilter_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/cvmsgspb //src/shared/cvmsgspb:cvmsgs_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/k8s/metadatapb //src/shared/k8s/metadatapb:metadata_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/metadatapb //src/shared/metadatapb:metadata_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/scriptspb //src/shared/scriptspb:scripts_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/services/jwtpb //src/shared/services/jwtpb:jwt_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/services/testproto //src/shared/services/testproto:ping_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/shared/types/typespb //src/shared/types/typespb:types_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/stirling/source_connectors/dynamic_tracer/dynamic_tracing/ir/logicalpb //src/stirling/source_connectors/dynamic_tracer/dynamic_tracing/ir/logicalpb:logical_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/stirling/source_connectors/socket_tracer/protocols/http2/testing/proto //src/stirling/source_connectors/socket_tracer/protocols/http2/testing/proto:greet_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/stirling/testing/demo_apps/go_grpc_tls_pl/server/greetpb //src/stirling/testing/demo_apps/go_grpc_tls_pl/server/greetpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/stirling/testing/demo_apps/hipster_shop/proto //src/stirling/testing/demo_apps/hipster_shop/proto:demo_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/table_store/schemapb //src/table_store/schemapb:schema_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/vizier/messages/messagespb //src/vizier/messages/messagespb:messages_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/vizier/services/metadata/metadatapb //src/vizier/services/metadata/metadatapb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/vizier/services/metadata/storepb //src/vizier/services/metadata/storepb:store_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/vizier/services/shared/agentpb //src/vizier/services/shared/agentpb:agent_pl_go_proto
#
# gazelle:resolve go px.dev/pixie/experimental/stirling/proto_data_model/proto //experimental/stirling/proto_data_model/proto:http_record_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/backend/sharedpb //src/e2e_test/perf_tool/backend/sharedpb:shared_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/backend/coordinator/coordinatorpb //src/e2e_test/perf_tool/backend/coordinator/coordinatorpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/experimentpb //src/e2e_test/perf_tool/experimentpb:experiment_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/backend/clustermgr/clustermgrpb //src/e2e_test/perf_tool/backend/clustermgr/clustermgrpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/backend/builder/builderpb //src/e2e_test/perf_tool/backend/builder/builderpb:service_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/artifactspb //src/e2e_test/perf_tool/artifactspb:artifacts_pl_go_proto
# gazelle:resolve go px.dev/pixie/src/e2e_test/perf_tool/backend/runner/runnerpb //src/e2e_test/perf_tool/backend/runner/runnerpb:service_pl_go_proto
#
# Make gazelle resolve CGO libraries correctly.
# gazelle:resolve go px.dev/pixie/src/carnot/goplanner //src/carnot/goplanner:go_default_library
# gazelle:resolve go px.dev/pixie/src/e2e_test/vizier/planner/dump_schemas/godumpschemas //src/e2e_test/vizier/planner/dump_schemas/godumpschemas:godumpschemas

# gazelle:map_kind go_binary pl_go_binary @px//bazel:pl_build_system.bzl
# gazelle:map_kind go_test pl_go_test @px//bazel:pl_build_system.bzl

buildifier(
    name = "buildifier",
    exclude_patterns = [
        "./third_party/**/*",
    ],
    lint_mode = "fix",
    lint_warnings = ["all"],
)

pl_cc_base_packages = [
    packages["libc6"],
    packages["libelf1"],
    packages["liblzma5"],
    packages["libtinfo6"],
    packages["libunwind8"],
    packages["zlib1g"],
]

# These filegroups aren't strictly necessary but nice to avoid the extra @*_model//file:, in any other rules that want
# to use these files.
filegroup(
    name = "sentencepiece.proto",
    srcs = ["@sentencepiece_model//file:sentencepiece.proto"],
    visibility = ["//visibility:public"],
)

filegroup(
    name = "embedding.proto",
    srcs = ["@embedding_model//file:embedding.proto"],
    visibility = ["//visibility:public"],
)

container_image(
    name = "pl_go_base_image_default",
    base = "@base_image//image",
    debs = pl_cc_base_packages,
)

container_image(
    name = "pl_cc_base_image_default",
    base = "@base_image//image",
    debs = pl_cc_base_packages,
    visibility = ["//visibility:public"],
)

sysroot_runtime_image(
    name = "pl_go_base_image",
    default_image = ":pl_go_base_image_default",
    visibility = ["//visibility:public"],
)

sysroot_runtime_image(
    name = "pl_cc_base_image_no_extra_files",
    default_image = ":pl_cc_base_image_default",
)

container_image(
    name = "pl_cc_base_image",
    base = ":pl_cc_base_image_no_extra_files",
    files = [
        "//:embedding.proto",
        "//:sentencepiece.proto",
    ],
    visibility = ["//visibility:public"],
)

container_image(
    name = "pl_cc_base_debug_image_default",
    base = "@base_image_debug//image",
    debs = pl_cc_base_packages,
    visibility = ["//visibility:public"],
)

sysroot_test_image(
    name = "pl_cc_base_debug_image_no_extra_files",
    default_image = ":pl_cc_base_debug_image_default",
)

container_image(
    name = "pl_cc_base_debug_image",
    base = ":pl_cc_base_debug_image_no_extra_files",
    files = [
        "//:embedding.proto",
        "//:sentencepiece.proto",
    ],
    visibility = ["//visibility:public"],
)

alias(
    name = "llvm",
    actual = select({
        llvm_variant_setting_label(variant): "@{repo}//:llvm".format(repo = llvm_variant_repo_name(variant))
        for variant in llvm_variants()
    }),
    visibility = ["//visibility:public"],
)

list_pl_deps(
    name = "pl_3p_deps",
)

filegroup(
    name = "pl_3p_go_sum",
    srcs = ["go.sum"],
    visibility = ["//visibility:public"],
)
